﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.Properties;

namespace Tank
{
    enum Tag
    {
        MyTank,
        EnemyTank
    }
    class Bullet : Moveing
    {
        public Tag Tag { get; set; }
        // 是否移除
        public bool IsDestroy { get; set; }
        //private Random r = new Random();
        public Bullet(int X, int y, int speet, Direction dir,Tag tag)
        {
            IsDestroy = false;
            this.X = X;
            this.Y = y;
            this.Speet = speet;

            this.BitmapDown = Resources.BulletDown;
            this.BitmapLeft = Resources.BulletLeft;
            this.BitmapRight = Resources.BulletRight;
            this.BitmapUp = Resources.BulletRight;
            this.Dir = dir;
            this.Tag = tag;
            this.X -= Width / 2;
            this.Y -= Height / 2;
        }

        public override void Update()
        {

            MoveCheck();
            Move();
            base.Update();
        }
        // 坦克越界检查
        private void MoveCheck()
        {
            #region 检测是否越界
            if (Dir == Direction.Up)
            {
                if (Y - Height / 2  + 3< 0)
                {
                    //GameObjectManager.DestroyBullet(this);
                    IsDestroy = true;
                    return;
                }
            }
            else if (Dir == Direction.down)
            {
                if (Y +  Height/2  - 3> 450)
                {
                    IsDestroy = true;
                    return;
                }

            }
            else if (Dir == Direction.Left)
            {
                if (X + Width/2 -3 < 0)
                {
                    IsDestroy = true;
                    return;
                }
            }
            else
            {
                if (X + Width/2 + 3 > 450)
                {
                    IsDestroy = true;
                    return;

                }
            }
            #endregion
            Rectangle rect = GetRectangle();
            rect.X = X + Width / 2 - 3;
            rect.Y = Y + Height / 2 - 3;
            rect.Height = 3;
            rect.Width = 3;
            //switch (Dir)
            //{
            //    case Direction.Up:
            //        rect.Y -= Speet;
            //        break;
            //    case Direction.down:
            //        rect.Y += Speet;
            //        break;
            //    case Direction.Left:
            //        rect.X -= Speet;
            //        break;
            //    case Direction.right:
            //        rect.X += Speet;
            //        break;
            //}
            // 两种墙体都不能碰撞
            //if (GameObjectManager.IsCollidedWall(rect) != null || GameObjectManager.IsCollidedSteel(rect) != null
            //    || GameObjectManager.IsCollidedBoss(rect) != null
            //    )
            //{
            //    //ChangeDirection();
            //    return;
            //}
            NotMoveing wall = null;
            int xExplsion = this.X + Width / 2;
            int yExplsion = this.Y + Height / 2;
            if ((wall = GameObjectManager.IsCollidedWall(rect)) != null)
            {
                IsDestroy = true;
                GameObjectManager.DestroyWall(wall);
                GameObjectManager.CreateExplosion(xExplsion, yExplsion);
                SoundManger.Balstlayer();
                return;
            }
            NotMoveing seteel = null;
            if((seteel = GameObjectManager.IsCollidedSteel(rect)) != null)
            {
                IsDestroy = true;
                return;
            }
            // 与boos发生碰撞的时候
            if (GameObjectManager.IsCollidedBoss(rect))
            {
               GameFrameWorl.ChangeToGameOver();
                SoundManger.Balstlayer();
                return;
            }
            if (Tag == Tag.MyTank)
            {
                EnemyTank tank = null;
                if((tank = GameObjectManager.IsCollidedEnemyTank(rect)) != null)
                {
                    IsDestroy = true;
                    GameObjectManager.DestroyTank(tank);
                    //GameObjectManager.CreateExplosion(xExplsion,yExplsion);
                }
            }
            else if(Tag == Tag.EnemyTank)
            {
                MyTank myTank = null;
                if ((myTank = GameObjectManager.IsCollidedMyTank(rect)) != null)
                {
                    IsDestroy = true;
                    GameObjectManager.CreateExplosion(xExplsion, yExplsion);
                    myTank.TakeDamage();
                 
                    return;
                }
            }
        }


        // 改变坦克朝向
        //private void changedirection()
        //{
        //    while (true)
        //    {
        //        Direction dir = (Direction)r.next(0, 4);
        //        if (dir == dir)
        //        {
        //            continue;
        //        }
        //        dir = dir;
        //        break;
        //    }
        //   movecheck();

        //}
        // 坦克移动
        private void Move()
        {

            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speet;
                    break;
                case Direction.down:
                    Y += Speet;
                    break;
                case Direction.Left:
                    X -= Speet;
                    break;
                case Direction.right:
                    X += Speet;
                    break;
            }
        }
    }
}
